const { log } = require('console');
var express = require('express');
var router = express.Router();
const multer = require('multer');
const { newsClass, news } = require('../model/newsModel')
const { upload } = require('../utils/upload')

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });

});
router.all('/gets', function (req, res, next) {
  res.json(req.query)
});
//新增新闻类型
router.post('/newsclass', function (req, res) {
  newsClass.create(req.body).then(function () {
    res.json({
      flag: true
    })
  })
})
//新增新闻内容
router.post('/news', upload.single('myfile'), function (req, res) {
  let path = req.file.destination + req.file.filename;
  path = path.slice(path.indexOf('/'));
  news.create({
    title: req.body.title,
    desc: req.body.desc,
    classid: req.body.sel,
    fileId: path
  }).then(function () {
    res.json({
      flag: true
    })
  })
})

//添加新闻标题
router.get('/getTitle', function (req, res) {
  newsClass.find().then(function (result) {
    res.json({
      flag: true,
      data: result
    })
  })
})

//添加表格渲染信息
router.get('/getnews', function (req, res) {
  news.find().populate('classid').then(function (result) {
    console.log(result);
    res.json({
      flag: true,
      data: result
    })
  })
})
module.exports = router;
